/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @filename: net_serv.h
 */

#ifndef __NET_SERV_H____
#define __NET_SERV_H____


#define net_new_transaction(net) ((net)->pkt_nr=0)

#include "vio.h"

#include <stdint.h>
#if !defined(__cplusplus)
# include <stdbool.h>
#endif

#define LIBCLIENT_ERRMSG_SIZE     512
#define LIBCLIENT_SQLSTATE_LENGTH 5

#ifdef __cplusplus
extern "C" {
#endif

typedef struct st_net {

  Vio *vio;

  unsigned char *buff;
  unsigned char *buff_end ;
  unsigned char *write_pos;
  unsigned char *read_pos;

  int fd;                    /* For Perl DBI/dbd */
  /*
    The following variable is set if we are doing several queries in one
    command ( as in LOAD TABLE ... FROM MASTER ),
    and do not want to confuse the client with OK at the wrong time
  */
  unsigned long remain_in_buf,length, buf_length, where_b;
  unsigned long max_packet,max_packet_size;
  unsigned int  pkt_nr,compress_pkt_nr;
  unsigned int  write_timeout;
  unsigned int  read_timeout;
  unsigned int  retry_count;
  int fcntl;
  unsigned int *return_status;
  unsigned char reading_or_writing;
  char save_char;
  bool compress;
  /*
    Pointer to query object in query cache, do not equal NULL (0) for
    queries in cache that have not stored its results yet
  */
  /*
    Unused, please remove with the next incompatible ABI change.
  */
  unsigned char *unused;
  unsigned int last_errno;
  unsigned char error;
  /** Client library error message buffer. Actually belongs to struct MYSQL. */
  char last_error[LIBCLIENT_ERRMSG_SIZE];
  /** Client library sqlstate buffer. Set along with the error message. */
  char sqlstate  [LIBCLIENT_SQLSTATE_LENGTH+1];
  void *extension;
} NET;

  bool client_net_init      (NET *net, Vio* vio);
  void client_net_local_init(NET *net);
  void client_net_end       (NET *net);
  void client_net_clear     (NET *net, bool clear_buffer);
  bool client_net_realloc   (NET *net, size_t length);
  bool client_net_flush     (NET *net);
  bool client_net_write     (NET *net,const unsigned char *packet, size_t len);
  bool client_net_write_command(NET *net,unsigned char command,
                               const unsigned char *header, size_t head_len,
                               const unsigned char *packet, size_t len);
  int32_t client_net_real_write(NET *net,
                                const unsigned char *packet,
                                size_t len);
  uint32_t client_net_read (NET *net);
  void     client_net_close(NET *net);
  bool     client_net_init_sock(NET * net, int sock, int flags);
  bool     client_net_peer_addr(NET *net,
                                char *buf,
                                uint16_t *port,
                                size_t buflen);
  void     client_net_keepalive(NET *net, bool flag);
  int      client_net_get_sd(NET *net);
  bool     client_net_should_close(NET *net);
  bool     client_net_more_data(NET *net);

  void     client_net_set_write_timeout(NET *net, uint32_t timeout);
  void     client_net_set_read_timeout(NET *net, uint32_t timeout);
  void     client_net_clear_error(NET *net);

 // typedef bool (*safe_read_error_hook_func)(NET *net);
 // extern safe_read_error_hook_func safe_read_error_hook;
#ifdef __cplusplus
}
#endif

#endif //__NET_SERV_H__
